#include "Boden.h"
#include "../SurfaceFunctions/surfacefunctions.h"
#include "../TextureFunctions/texturefunctions.h"
#include "../DataTypes/datatypes.h"
#include "../GlobalVars/vars.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;

// Laenge und Breite des Boden
const int laenge = 300;
const int breite = 300;

// Vertex, Index usw. fuer den Boden 
static datatypes::Vector3D bodenVertices[(laenge+1)*(breite+1)];
static unsigned int        bodenIndices[(laenge+1)*(breite+1)*6];
static datatypes::Vector3D bodenNormals[(laenge+1)*(breite+1)];
static datatypes::Vector2D bodenTexCoord[(laenge+1)*(breite+1)];

const int glaenge = 150;
const int gbreite = 150;

//Vertex usw. fuer die Begrenzung am Meer
static datatypes::Vector3D gruenVertices[(glaenge+1)*(gbreite+1)];
static unsigned int        gruenIndices[(glaenge+1)*(gbreite+1)*6];
static datatypes::Vector3D gruenNormals[(glaenge+1)*(gbreite+1)];
static datatypes::Vector2D gruenTexCoord[(glaenge+1)*(gbreite+1)];


ostream& operator<<(ostream &os,const datatypes::triangle &vec)
{
  os << vec.x << ", " << vec.y << ", " << vec.z << " ";
  return os;
}



void init_boden()
{
  // Beton-Boden
  surfacefunctions::generateSurface(laenge,breite,0.5,0.5,bodenVertices,bodenIndices);
  surfacefunctions::computeNormals(laenge,breite,bodenVertices,bodenIndices,bodenNormals);
  surfacefunctions::generateTexCoords(laenge,breite,bodenVertices,bodenTexCoord);
  // Gruenflache
  surfacefunctions::generateSurface(glaenge,gbreite,1.0,1.0,gruenVertices,gruenIndices);
  surfacefunctions::computeNormals(glaenge,gbreite,gruenVertices,gruenIndices,gruenNormals);
  surfacefunctions::generateTexCoords(glaenge,gbreite,gruenVertices,gruenTexCoord);

 

}
void draw_boden_display()
{
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_NORMAL_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glVertexPointer(3,GL_FLOAT,0,bodenVertices);
  glNormalPointer(GL_FLOAT,0,bodenNormals);
  glTexCoordPointer(2,GL_FLOAT,0,bodenTexCoord);
  
  OGL_Set_Current_Texture_Map(boden,0,0,0,0);
  OGL_Set_Current_Material_Parameters(32.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);
  for(int i=0;i<(laenge+1)*(breite+1)*2;i++)
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, &bodenIndices[i*3]);
  //glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, &bodenIndices[3]);

  /* glNormalPointer(GL_FLOAT,0,gruenNormals);
  glVertexPointer(3,GL_FLOAT,0,gruenVertices);
  glTexCoordPointer(2,GL_FLOAT,0,gruenTexCoord);*/
  glTranslatef(0,0,150);
  glRotatef(90,1,0,0);
  //OGL_Set_Current_Texture_Map(pallete,0,0,0,0);
  OGL_Set_Current_Material_Parameters(32.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  for(int i=0;i<(laenge+1)*(breite+1)*2;i++)
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, &bodenIndices[i*3]);

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_NORMAL_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	
}


void draw_boden()
{
 
  
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_INDEX_ARRAY);
  glEnableClientState(GL_NORMAL_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  // Texture fuer Boden setzen
  OGL_Set_Current_Texture_Map(boden,0,0,0,0);
  OGL_Set_Current_Material_Parameters(32.0000f, 1.00000f, 1.00000f, 1.00000f, 1.00000f, 1.00000f, 1.00000f);
  
  glNormalPointer(GL_FLOAT,0,bodenNormals);
  glVertexPointer(3,GL_FLOAT,0,bodenVertices);
  glTexCoordPointer(2,GL_FLOAT,0,bodenTexCoord);
  
  OGL_Set_Current_Material_Parameters(1,0.5,0.5,0.5,0.5,0.5,0.5);
  
  glDrawElements(GL_TRIANGLES,(laenge+1)*(breite+1)*6,GL_UNSIGNED_INT,bodenIndices);

  glTranslatef(0,0,0);
  glRotatef(90,1,0,0);
  OGL_Set_Current_Texture_Map(pallete,0,0,0,0);
  glNormalPointer(GL_FLOAT,0,gruenNormals);
  glVertexPointer(3,GL_FLOAT,0,gruenVertices);
  glTexCoordPointer(2,GL_FLOAT,0,gruenTexCoord);
  
  glDrawElements(GL_TRIANGLES,(glaenge+1)*(gbreite+1)*6,GL_UNSIGNED_INT,gruenIndices);
 
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  glDisableClientState(GL_INDEX_ARRAY);
  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_NORMAL_ARRAY);
 
}
